﻿Imports GBDZafiro

Public Class FrmTalla

    Private Sub FrmTalla_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargargvTalla()
    End Sub
    
    Protected Overrides Sub bnNuevo_Click(sender As System.Object, e As System.EventArgs)
        MyBase.bnNuevo_Click(sender, e)
        tbCodTalla.Focus()
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As System.Object, e As System.EventArgs)
        If validarCampos() Then
            Exit Sub
        End If
        updateAddTalla()
        cargargvTalla()
        MyBase.bnGuardar_Click(sender, e)
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As System.Object, e As System.EventArgs)
        If tbCodTalla.Text = "NA" Or tbCodTalla.TextLength < 1 Then
            msnBox.msnExclamacion("Elija una Talla")
            Exit Sub
        End If
        deleteTalla()
        cargargvTalla()
        MyBase.bnEliminar_Click(sender, e)
    End Sub

    Protected Overrides Sub bnEditar_Click(sender As System.Object, e As System.EventArgs)
        MyBase.bnEditar_Click(sender, e)
        tbCodTalla.Enabled = False
    End Sub

    Private Sub gvTalla_RowHeaderMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gvTalla.RowHeaderMouseClick
        selectTalla()
    End Sub

    Private Sub gvTalla_CurrentCellChanged(sender As System.Object, e As System.EventArgs) Handles gvTalla.CurrentCellChanged
        selectTalla()
    End Sub

    Private Sub selectTalla()
        Try
            Dim rowgvTalla As New DataGridViewRow()
            rowgvTalla = gvTalla.CurrentRow
            tbCodTalla.Text = rowgvTalla.Cells.Item("COD_TALLA").Value.ToString()
            tbDescripcion.Text = rowgvTalla.Cells.Item("DESCRIPCION").Value.ToString()
        Catch ex As Exception

        End Try
    End Sub

    Private Function validarCampos() As Boolean
        If tbCodTalla.TextLength < 1 Or tbDescripcion.TextLength < 1 Then
            msnBox.msnExclamacion("Complete todos los campos")
            Return True
        Else
            Return False
        End If
    End Function

    Private Function crearTalla() As Talla
        Dim talla As New Talla
        talla.codTalla = tbCodTalla.Text.ToUpper()
        talla.descripcion = tbDescripcion.Text.ToUpper()
        Return talla
    End Function

    Private Sub updateAddTalla()
        Dim talla As Talla = crearTalla()
        If GBDZafiro.Talla.exisTalla(talla) Then
            GBDZafiro.Talla.Actualizar(talla)
            'msnBox.msnInformacion("Talla Actualizada")
        Else
            GBDZafiro.Talla.Nuevo(talla)
            'msnBox.msnInformacion("Talla Guardada")
        End If
    End Sub

    Private Sub deleteTalla()
        If msnBox.msnQuestion("¿ Desea eliminar la talla " + tbCodTalla.Text + " ?") = _
            Windows.Forms.DialogResult.Yes Then
            Dim talla As Talla = crearTalla()
            GBDZafiro.Talla.Eliminar(talla)
            msnBox.msnInformacion("Talla Borrada")
        End If
    End Sub

    Private Sub cargargvTalla()
        gvTalla.DataSource = Nothing
        gvTalla.Rows.Clear()
        gvTalla.DataSource = Talla.ConsultarAll()
        gvTalla.Columns(0).HeaderText = "CODIGO TALLA"
    End Sub

End Class
